import store from "@/store";
import Vue from "vue";
import VueRouter from "vue-router";

// 导入所需组件
// const Login = ()=>{
//   return import('@/views/login/index.vue')
// }

// const login = () => import("@/views/login/index.vue");

Vue.use(VueRouter);

const routes = [
  { path: "/login", component: () => import("@/views/login/index.vue") },
  {
    path: "/",
    component: () => import("@/views/layout/index.vue"),
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard", component: () => import("@/views/dashboard/index.vue"),
      },
      { path: "article", component: () => import("@/views/article/index.vue") },
    ],
  },
];

const router = new VueRouter({
  routes,
});

// 加入导航守卫，如果没有登陆则不允许访问其他页面
router.beforeEach((to, from, next) => {
  // to.path 要访问的地址
  // from.path 从哪里来
  // next() 放行
  // next('路径') 拦截至路径
  if (to.path !== '/login' && !store.state.user.token) {
    next('/login')
    return
  }
  next()
})

export default router;
